package com.dempsey.gateway.filter;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @author Dempsey
 * Date 2022/5/24
 * Description
 */
@Component
public class TokenBucketResolver {

    @Bean
    public KeyResolver ipKeyResolver() {
        return exchange -> {
            // ip
            String ipAddr = exchange.getRequest().getRemoteAddress().getHostName();

            // url
            String path = exchange.getRequest().getPath().toString();

            System.out.println("经过了限流, ipAddr: " + ipAddr + " , path: " + path);

            // redis key
            String key = ipAddr + path;

            return Mono.just(key);
        };
    }

}
